layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };

    /**
     * 初始化表格的列
     */
    TDispatch.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'getUnit', title: '收货客户', style:'font-weight: 600;',width:180},
            {field: 'trainNumber', title: '车次', style:'font-weight: 600;'},
            {field: 'toovolume', title: '总方数m3', style:'font-weight: 600;'},
            {field: 'toomoney', title: '总货值', style:'font-weight: 600;'},
            {field: 'pieceNumberSum', title: '整件+拆零封箱数合计', style:'font-weight: 600;'},
            {field: 'kilometer', title: '公里数', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 导出excel按钮
     */
/*    TDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TDispatch.tableId);
        var originalColumns = TDispatch.initColumn()[0];
        var dataToExport = checkRows.data;
        // 移除数据
        dataToExport = dataToExport.map(function (row){
            delete row.id;
            delete row.center;
            row.dispatchNumber = row.dispatchNumber.trim();
            return row;
        })
        // 移除表头
        originalColumns = originalColumns.filter(column => {
            return column.field !== 'id' && column.field !== 'center';
        })

        if (checkRows.data.length > 0) {
            layer.confirm('请选择导出格式：', {
                btn: ['xlsx', 'xls'],
                skin: 'layui-layer-molv'
            }, function (index) {
                // exportToXlsx(dataToExport, originalColumns, '承运商装车明细.xlsx');
                tableToExcel(dataToExport, originalColumns, '承运商装车明细.xlsx');
                layer.close(index);
            },function (index){
                table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                layer.close(index);
            });
        } else {
            var queryData = {};
            queryData['operateId'] = $("#operateId").val();
            queryData['operateName'] = $("#operateName").val();
            queryData['chexing'] = $("#chexing").val();
            queryData['getUnit'] = $("#getUnit").val();
            queryData['condition'] = $("#condition").val();
            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['carrierId'] = $("#carrierId").val();
            queryData['carrierName'] = $("#carrierName").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/tDispatch/findchengyunshangMingxiAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    var allData = checkRows.data;
                    allData = allData.map(function (row){
                        delete row.id;
                        delete row.center;
                        row.dispatchNumber = row.dispatchNumber.trim();
                        return row;
                    })
                    layer.confirm('请选择导出格式：', {
                        btn: ['xlsx', 'xls'], // 按钮
                        skin: 'layui-layer-molv'
                    }, function (index) {
                        // exportToXlsx(allData, originalColumns, '承运商装车明细.xlsx');
                        tableToExcel(allData, originalColumns, '承运商装车明细.xlsx');
                        layer.close(index);
                    },function (index){
                        table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                        layer.close(index);
                    })
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });

        }
    };*/

    // 导出excel
    $('#btnExp').click(function () {
        TDispatch.exportExcel();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['operateName'] = $("#operateName").val();
        queryData['chexing'] = $("#chexing").val();
        queryData['getUnit'] = $("#getUnit").val();
        queryData['condition'] = $("#condition").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['carrierId'] = $("#carrierId").val();
        queryData['carrierName'] = $("#carrierName").val();
        table.reload(TDispatch.tableId, {
            where: queryData, page: {curr: 1},
            done: function (res, curr, count) {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch.tableId,
        url: Feng.ctxPath + '/tDispatch/findchengyunshangMingxiPage',
        page: true,
        limit: 60,
        height: "full-120",
        // totalRow: true,
        // toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TDispatch.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
        $('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
        $('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

    // 工具条点击事件
    table.on('tool(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'tooDetailed') {
            layer.open({
                type: 2,
                title: '配送派车信息',
                area: ['1250px', '570px'],
                anim: 5,
                content: Feng.ctxPath + '/tDispatch/tooDetailed?dispatchNumber=' + data.dispatchNumber,
            });
        } else if (layEvent === 'tooConsultMoneyDetailed') {
            layer.open({
                type: 2,
                title: '费用信息',
                area: ['1100px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/transportCost?dispatchNumber=' + data.dispatchNumber,
            });
        }
    });

});
